package com.kxq.zhuoyue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kxq.zhuoyue.po.EmpCode;
import com.kxq.zhuoyue.service.EmpCodeService;
import com.kxq.zhuoyue.util.result.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/code")
public class EmpCodeController {
    @Autowired
    private EmpCodeService empCodeService;

//    @GetMapping("/selectEmpCode")
//    public Result findAll(){
//        List<EmpCode> list = empCodeService.list();
//        return Result.success(list);
//    }

    @PostMapping("/findByPage")
    public AjaxResult findByPage(@RequestBody EmpCode empCode){
        Page<EmpCode> page = new Page<>(empCode.getPageNum(),empCode.getPageSize());
        IPage<EmpCode> ipage = empCodeService.page(page);
        List<EmpCode> list = ipage.getRecords();
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/findByName")
    public AjaxResult findByName(@RequestBody EmpCode empCode){
        //创建QueryWrapper查询对象
        QueryWrapper<EmpCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("code_name",empCode.getCodeName()); //查询条件 dname 等于 hello
        Page<EmpCode> page = new Page<>(empCode.getPageNum(),empCode.getPageSize());
        IPage<EmpCode> ipage = empCodeService.page(page,queryWrapper);
        return AjaxResult.success("查询成功",ipage.getRecords());
    }

    @PostMapping("/findById")
    public AjaxResult findById(@RequestBody EmpCode empCode){
        QueryWrapper<EmpCode> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("code_id",empCode.getCodeId());
        List<EmpCode> list = empCodeService.list(queryWrapper);
        return AjaxResult.success("查询成功",list);
    }

    @PostMapping("/addEmpCode")
    public AjaxResult add(@RequestBody EmpCode empCode){
        boolean add = empCodeService.save(empCode);
        if(add){
            return AjaxResult.success("添加成功",add);
        }else{
            return AjaxResult.error("添加失败",add);
        }

    }

}
